using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using Campus.Entities;
using CampusSite.Security;
namespace CampusSite.Models
{

    [RolActionFilter]
    public class ResponsableRepository : IResponsableRepository
    {
        //CampusSiteContext context = new CampusSiteContext();

        public CampusSiteContext context { get; set; }

        #region Constructores
        
        public ResponsableRepository()
        {
            context = new CampusSiteContext();
        
        }

        public ResponsableRepository(CampusSiteContext context)
        {
            this.context = context;

        }

        #endregion
        public IQueryable<Responsable> All
        {
            get { return context.Responsables; }
        }

        public IQueryable<Responsable> AllIncluding(params Expression<Func<Responsable, object>>[] includeProperties)
        {
            IQueryable<Responsable> query = context.Responsables;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public Responsable Find(int id)
        {
            return context.Responsables.Find(id);
        }

        public Responsable FindByUser(string UserName)
        {
            return context.Responsables.Where(r => r.Usuario != null && r.Usuario.UserName == UserName && r.Usuario.Confirmado).SingleOrDefault();
        }
        
        public void InsertOrUpdate(Responsable responsable)
        {
            if (responsable.ResponsableId == default(int)) {
                // New entity
                context.Responsables.Add(responsable);
            } else {
                // Existing entity
                context.Entry(responsable).State = EntityState.Modified;
            }
        }




        public void Delete(int id)
        {
            var responsable = context.Responsables.Find(id);
            context.Responsables.Remove(responsable);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose() 
        {
            context.Dispose();
        }

        public Responsable FindAsNoTracking(int idEstudiante)
        {
            return context.Responsables.AsNoTracking().Where(r => r.Estudiantes.Any(e => e.EstudianteId.Equals(idEstudiante))).FirstOrDefault();
        }

        public Responsable FindResponsableAsNoTracking(int id)
        {
            return context.Responsables.AsNoTracking().Where(r => r.ResponsableId == id).SingleOrDefault();
        }

        public bool ExisteResponsable(Responsable responsable)
        {             
            var existente = context.Responsables
            .Where(r => ((r.DNI == responsable.DNI && (responsable.DNI != 0 && responsable.DNI != 1)) ||
                         (r.Email.ToLower() == responsable.Email.ToLower() && (!string.IsNullOrEmpty(responsable.Email))) ||
                         (r.Apellido.ToLower() == responsable.Apellido.ToLower() && r.Nombre.ToLower() == responsable.Nombre.ToLower())) &&
                         (r.ResponsableId != responsable.ResponsableId)).FirstOrDefault();                                                                      
            return (existente != null);            
        }
    }

    public interface IResponsableRepository : IDisposable
    {
        IQueryable<Responsable> All { get; }
        IQueryable<Responsable> AllIncluding(params Expression<Func<Responsable, object>>[] includeProperties);
        Responsable Find(int id);
        void InsertOrUpdate(Responsable responsable);
        void Delete(int id);
        void Save();
        Responsable FindAsNoTracking(int id);
        Responsable FindByUser(string UserName);
        Responsable FindResponsableAsNoTracking(int id);
        bool ExisteResponsable(Responsable responsable);

    }
}